package imitate;

import java.util.HashMap;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-02-02
 * Time:18:32
 * Text：数青蛙
 */
public class Text3 {
    public  static int minNumberOfFrogs(String croakOfFrogs) {
        HashMap<Character,Integer> map=new HashMap<>();
        HashMap<Character,Integer> map1=new HashMap<>();
        char[] arr=croakOfFrogs.toCharArray();
        map1.put('c',0);map1.put('r',1);map1.put('o',2);map1.put('a',3);map1.put('k',4);
        map.put('c',0);map.put('r',0);map.put('o',0);map.put('a',0);map.put('k',0);
        String str1="croak";
        for(int i=0;i<arr.length;i++){
            char a=arr[i];
            //如果不包含此字符，则返回-1
            if(map.getOrDefault(a,-1)==-1){
                return -1;
            }
            //如果从c开始，两种情况，一种，一个青蛙已经呱好了，另一种又有青蛙呱了
            if(a =='c'){
                if(map.get('k')!=0){
                    map.put('k',map.get('k')-1);
                }
                map.put('c',map.get(a)+1);
            }else{
                //判断前驱是否存在
                if(map.get(str1.charAt(map1.get(a)-1))==0){
                    return -1;
                }
                map.put(a,map.get(a)+1);
                map.put(str1.charAt(map1.get(a)-1),map.get(str1.charAt(map1.get(a)-1))-1);
            }
        }
        for (char key : map.keySet()) {
            if(key=='k'){
                continue;
            }
            if(map.get(key)!=0){
                return -1;
            }
        }
        return map.get('k');
    }

    public static void main(String[] args) {
        System.out.println(minNumberOfFrogs("crcoakrook"));
        ;
    }
}
